package blue.squares;

import android.app.Dialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

/**
 * @author Luisa Lu
 */
public class PreferenceDialog extends Dialog {
    
    // ------------------------------------------------------------------------
    // Constants.
    // ------------------------------------------------------------------------
    
    // Key for various options
    private static final String KEY_BOARD_SIZE = "BoardSize";
    private static final String KEY_PLAYER2_TYPE = "Player2";

    // Default value for various options
    private static final int DEFAULT_BOARD_SIZE_INDEX = 2;    
    private static final int DEFAULT_PLAYER2_TYPE_INDEX = 0; 
    
    // List of options
    private static final String[] BOARD_SIZE_STR = 
            new String[]{"5x5", "6x6", "7x7", "8x8"};
    
    private static final int[] BOARD_SIZE_INT = 
            new int[]{5, 6, 7, 8};
    
    private static final String[] PLAYER_TYPE_STR = 
            new String[]{"Human", "Random Computer"};
    
    private static final int[] PLAYER_TYPE_INT =
            new int[]{Game.HUMAN_PLAYER, Game.NOVICE_COMPUTER};
    
    // ------------------------------------------------------------------------
    // Fields.
    // ------------------------------------------------------------------------
    
    Squares _squares;
    SharedPreferences _sharedPrefs;
    
    // Save current selection in memory
    int _currentBoardSizeIndex;
    int _currentPlayer2TypeIndex;
    
    // ------------------------------------------------------------------------
    // Implementation.
    // ------------------------------------------------------------------------
    
    public PreferenceDialog(Squares context){
        super(context);
        _squares = context;
    }
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Initialize current values
        _currentBoardSizeIndex = getPreferenceInt(KEY_BOARD_SIZE, DEFAULT_BOARD_SIZE_INDEX);
        _currentPlayer2TypeIndex = getPreferenceInt(KEY_PLAYER2_TYPE, DEFAULT_PLAYER2_TYPE_INDEX);
                
        setCancelable(true);
        setCanceledOnTouchOutside(false);
        setTitle(R.string.settings);
        setContentView(R.layout.preference_dialog);
        setupDialog();
    }
    
    /**
     * Setup and initialize the widgets in the dialog
     * @param dialog
     */
    private void setupDialog(){
        ArrayAdapter<CharSequence> adapter;
        
        Spinner boardSizeSpinner = (Spinner)findViewById(R.id.board_size_spinner);
        adapter = new ArrayAdapter<CharSequence>(_squares, 
                android.R.layout.simple_spinner_item, BOARD_SIZE_STR);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        boardSizeSpinner.setAdapter(adapter);
        boardSizeSpinner.setSelection(_currentBoardSizeIndex);
        boardSizeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
            public void onItemSelected(AdapterView adapterview, View view, int i, long l) {    
                _currentBoardSizeIndex = i;
            }
            public void onNothingSelected(AdapterView<?> arg0) {
                // no-op
            }
        });

        Spinner playerTypeSpinner = (Spinner)findViewById(R.id.player_type_spinner);
        adapter = new ArrayAdapter<CharSequence>(_squares, 
                android.R.layout.simple_spinner_item, PLAYER_TYPE_STR);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        playerTypeSpinner.setAdapter(adapter);
        playerTypeSpinner.setSelection(_currentPlayer2TypeIndex);
        playerTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
            public void onItemSelected(AdapterView adapterview, View view, int i, long l) {    
                _currentPlayer2TypeIndex = i;
            }
            public void onNothingSelected(AdapterView<?> arg0) {
                // no-op
            }
        });
        
        Button okButton = (Button)findViewById(R.id.preference_save);
        okButton.setOnClickListener(new View.OnClickListener(){
            public void onClick(View arg0) {
                okClicked();
            }
        });        
    }    
    
    private void okClicked(){ 
        Util.logDebug(this, "okClicked");
        savePreferences();
        dismiss();
    }
    
    private int getPreferenceInt(String key, int defaultValue){
        if(_sharedPrefs == null ){
            _sharedPrefs = PreferenceManager.getDefaultSharedPreferences(_squares);
        }
        return _sharedPrefs.getInt(key, defaultValue);
    }    
    
    private void savePreferences() {
        Editor editor = _sharedPrefs.edit();
        editor.putInt(KEY_BOARD_SIZE, _currentBoardSizeIndex);
        editor.putInt(KEY_PLAYER2_TYPE, _currentPlayer2TypeIndex);
        editor.commit();
    }
    
    /**
     * Get board size setting from preference (not in memory)
     * @return
     */
    public static int getBoardSize(Context context){     
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        int index = sharedPrefs.getInt(KEY_BOARD_SIZE, DEFAULT_BOARD_SIZE_INDEX);
        return BOARD_SIZE_INT[index];
    }
    
    /**
     * Get player 2 type setting from preference (not in memory)
     * @return
     */
    public static int getPlayer2Type(Context context){
        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
        int index = sharedPrefs.getInt(KEY_PLAYER2_TYPE, DEFAULT_PLAYER2_TYPE_INDEX);
        return PLAYER_TYPE_INT[index];
    }
}
